<?php
namespace app\blog\controller;
use cmf\controller\AdminBaseController;
use app\blog\model\StorecatModel as cat;
use think\Db;

class StoreCatController extends AdminBaseController
{    
    public function index(){
        $cat_list = Db::name('StoreType')->paginate(10);
        //dump($cat_list);
        // 获取分页显示
        $page = $cat_list->render();
        $this->assign("page", $page);
        $this->assign("cat_list", $cat_list);
        return $this->fetch();
    }
    /*
     * 2018 0511  再添加店铺时候直接给配套一套商品
     * */
    public function add(){
        $goods_list = Db::name('Goods')->where(array('g_status'=>1))->select()->toarray();
        $check=Db::name('StoreType')->count();
        $this->assign("check", $check);
        $this->assign("goods_list", $goods_list);
        return $this->fetch();
    }
    /*
     * 2018/05/25 王磊 根据系列添加商品
     * */
    public function goodsadd(){
        //获取规格
        $brand=DB::name('format')->where(['status'=>1])->select();
        //获取类别
        $classify=Db::name('GoodsClassify')->where(array('status'=>1))->select();
        $this->assign("classify", $classify);
        $this->assign("brand", $brand);
        return $this->fetch('store_cat/goodsadd');
    }
    /*
   * 添加商品模板
   * 没添加一个商品就对所有商铺类型添加
   * */
    /*public function goodspost(){
            $res  = $this->request->param();
            if (!empty($res['photo_names']) && !empty($res['photo_urls'])){
                $res['post']['more']['photos'] = [];
                foreach ($res['photo_urls'] as $key => $url){
                    $photoUrl = cmf_asset_relative_url($url);
                    array_push($res['post']['more']['photos'], ["url" => $photoUrl, "name" => $res['photo_names'][$key]]);
                }
            }
            if(empty($res['options']['g_goodsname'])){
                $this->error("商品名称不能为空！");
                exit;
            }
            if(empty($res['options']['price'])){
                $this->error("您还没有填写价格！");
                exit;
            }
            if($res['format']==1)
            {
                $format='大';
            }else{
                $format='中';
            }
            //查询商铺类型
            $storetype=DB::name('store_type')->select();
            $array=array();
            foreach($storetype as $k=>$v)
            {
                $array[$k]['g_goodsname']=$res['options']['g_goodsname'];
                $array[$k]['g_pic']=$res['photo_urls'][0];
                $array[$k]['pinyin']=$res['pinyin'];
                $array[$k]['g_price']=$res['options']['price'];
                $array[$k]['g_format']=$format;
                $array[$k]['g_vipprice']=$res['options']['vipprice'];
                $array[$k]['g_category']=$res['g_category'];
                $array[$k]['g_status']=1;
                $array[$k]['g_utime']=time();
                $array[$k]['num']=0;
                $array[$k]['stortype']=$v['id'];
                $array[$k]['brandid']=$res['brand'];
                $array[$k]['storeid']=0;
                $array[$k]['g_ctime']=time();
            }
            $re=DB::name('goods_table')->insertAll($array);
            if($re){
                $this->success("添加成功！", url("Goods/index"));
            }else{
                $this->error("添加失败，请重新添加！", url("Goods/add"));
            }

    }*/
    /*
     * 2018/05/22 王磊  添加店铺类型时候直接在商品模板中添加商品
     * */
    public function addPost(){
        $cat=new cat();
        $res  = $this->request->param();
        $data=array(
            'franchise_fee'=>$res['franchise_fee'],
            'cat_name'=>$res['cat_name'],
        );
        $re=Db::name('store_type')->insert($data);
        if($re){
            $this->success('添加成功！','storecat/index');
        }else{
            $this->error('添加失败！','storecat/add');
        }
    }
    public function edit(){
        $id  = $this->request->param('id');
        $info = Db::name('StoreType')->where(array('id'=>$id))->find();
        $goods_list = Db::name('Goods')->where(array('g_status'=>1))->select()->toarray();
        $this->assign("goods_list", $goods_list);
        $this->assign("info", $info);
        return $this->fetch();
    }
    /*
     * 店铺类别修改
     * */
    public function editPost(){
        $res  = $this->request->param();
        $data=array(
            'franchise_fee'=>$res['franchise_fee'],
            'cat_name'=>$res['cat_name'],
        );
        $re=Db::name('StoreType')->where(array('id'=>$res['id']))->update($data);
        if($re){
            $this->success('编辑成功！','Store/index');
        }else{
            $this->success('编辑失败！','Store/add');
        }
    }
    function delete(){
        $id = $this->request->param("id");
        //判断这个类别下有没有店铺
        $check=Db::name('Store')->where(array('cat_store'=>$id))->find();
        if($check){
            $this->error('该类型下面有店铺，不能删除');
            exit;
        }
        $info = Db::name('StoreType')->where(['id' => $id])->find();
        if ($info){
            $status = Db::name('StoreType')->delete($id);
            if (!empty($status)) {
                $this->success("删除成功！", url('StoreCat/index'));
            } else {
                $this->error("删除失败！");
            }
        }else{
            $this->error("该类别不存在！");
        }   
    }
    /*
     * 品项管理
     * */
    public function brand(){
        $cat=new cat();
        $brand=$cat->brand();
        $this->assign('brand',$brand);
        return $this->fetch('store_cat/brand');
    }
    /*
     * 添加
     * */
    public function addbrand(){
        $cat=new cat();
        $data=$this->request->post();
        if($data !=null)
        {
           $data['ctime']=time();
           $data['utime']=time();
           $satet=DB::name('brand')->insert($data);
           if($satet){
               return $this->success('添加成功',url('storeCat/brand'));
           }else{
             return $this->error('添加失败',url('storeCat/addbrand'));
           }
        }else{

            return $this->fetch('store_cat/addbrand');
        }

    }
    /*
     * 删除
     * */
    public  function branddel()
    {
          $id=$this->request->param();
          $status=DB::name('brand')->where($id)->delete();
        if($status){
            return $this->success('删除成功',url('storeCat/brand'));
        }else{
            return $this->error('删除失败',url('storeCat/brand'));
        }
    }
    /*
     * 更新
     * */
    public  function updatebrand()
    {
        $id=$this->request->param();
        $data=$this->request->post();
        if($data)
        {
            $where['id']=$data['id'];
            $state=DB::name('brand')->where($where)->update($data);
            if($state)
            {
                return $this->success('更新成功',url('storeCat/brand'));
            }else{
                return $this->error('更新失败',url('storeCat/updatebrand'));
            }
        }else{
            $status=DB::name('brand')->where($id)->find();
            $this->assign('date',$status);
            return $this->fetch('store_cat/update');
        }

    }
}
